package cn.catsdad.convert.bean;

import cn.catsdad.util.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 转换配置
 *
 * @author xuyl
 * @version V1.0.0 2024/12/31
 * @since V100R001
 */
@Data
@Accessors(chain = true)
public class CovertConfig {

    /**
     * sql文件夹路径
     */
    private String dirPath;

    /**
     * 是否插入truncate语句
     */
    private boolean isTruncate;

    /**
     * 转换后文件输出路径
     */
    private String outPath;

    /**
     * 批处理数量
     */
    private int batchSize;

    /**
     * 目标数据库名
     */
    private String targetDatabaseName;

    /**
     * 源数据库名
     */
    private String sourceDatabaseName;

    /**
     * 线程数量
     */
    private int threadCount;

    /**
     * 是否打印运行sql
     */
    private boolean isPrintRunSql;

    public static CovertConfig getInstance() {
        CovertConfig result = new CovertConfig()
                .setTruncate(ResourceUtil.readOrDefault("isTruncate", Boolean.class, true))
                .setBatchSize(ResourceUtil.readOrDefault("batchSize", Integer.class, 10000))
                .setDirPath(ResourceUtil.readResource("filePath", String.class))
                .setOutPath(ResourceUtil.readResource("outPath", String.class))
                .setTargetDatabaseName(ResourceUtil.readOrDefault("targetDatabaseName", String.class, null))
                .setSourceDatabaseName(ResourceUtil.readOrDefault("sourceDatabaseName", String.class, null))
                .setThreadCount(ResourceUtil.readOrDefault("threadCount", Integer.class, Runtime.getRuntime().availableProcessors()))
                .setPrintRunSql(ResourceUtil.readOrDefault("isPrintRunSql", Boolean.class, false));
        if (StrUtil.isEmpty(result.getDirPath())) {
            throw new RuntimeException("必须配置文件路径！");
        }
        return result;
    }
}
